package com.szdvte.news.util;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.szdvte.news.bean.IndexEntity;

@SuppressWarnings("serial")
public class MyTag extends TagSupport 
{
	//**********调用标签要传给标签的属性**********/
	@SuppressWarnings("rawtypes")
	private List items;
	private String var;
	private int pageSize;
	private int pageNo;
	private String url;
	private int row;
	private String varStatus;
	public String getVarStatus() 
	{
		return varStatus;
	}

	public void setVarStatus(String varStatus) 
	{
		this.varStatus = varStatus;
	}


	@SuppressWarnings("unused")
	private IndexEntity index;

	public int getRow() 
	{
		return row;
	}

	public void setRow(int row) 
	{
		this.row = row;
	}

	public String getUrl() 
	{
		return url;
	}

	public void setUrl(String url) 
	{
		this.url = url;
	}


	//************标签内部计算的值************//
	private int startIndex;
	private int endIndex;
	private int totalPages;
	
	//当前即将输出数据的索引
	private int currentIndex;
	
	@Override
	public int doStartTag() throws JspException
	{
		if(items==null)
		{
			return SKIP_BODY;
		}
		
		if(items.size()>0)
		{
			if(pageNo==0)
			{
				pageNo=1;
			}
			if(pageSize==0)
			{
				pageSize=10;
			}
			//计算总页数
			if(items.size()%pageSize==0)
			{
				totalPages=items.size()/pageSize;
			}
			else
			{
				totalPages=items.size()/pageSize+1;
			}
			//计算当前页的开始索引
			startIndex=(pageNo-1)*pageSize;
			//纯属防止外界恶意传值
			if(startIndex<0)
			{
				startIndex=0;
			}
			if(startIndex>items.size()-1)
			{
				startIndex=items.size()-1;
			}
			//计算当前也的结束索引
			if(pageNo==totalPages){
				endIndex=items.size()-1;
			}else{
				endIndex=pageNo*pageSize-1;
			}
			
			//纯属防止外界恶意传值
			if(endIndex<0)
			{
				endIndex=0;
			}
			if(endIndex>items.size()-1)
			{
				endIndex=items.size()-1;
			}
			//当前索引应该从开始索引开始
			currentIndex=startIndex;
			this.pageContext.setAttribute(var, items.get(currentIndex));
			if(varStatus!=null)
			{
				this.pageContext.setAttribute(varStatus, new IndexEntity(currentIndex));
			}
			return EVAL_BODY_INCLUDE;
		}
		else
		{
			return SKIP_BODY;
		}
		
	}
	
	@Override
	public int doAfterBody() throws JspException
	{
		//因为已经执行了一次标签体
		currentIndex++;
		if(currentIndex>endIndex)
		{
			return SKIP_BODY;
		}
		this.pageContext.setAttribute(var, items.get(currentIndex));
		if(varStatus!=null)
		{
			this.pageContext.setAttribute(varStatus, new IndexEntity(currentIndex));
		}
		return EVAL_BODY_AGAIN;
	}
	public int doEndTag() throws JspException
	{
		JspWriter out = this.pageContext.getOut();
		try 
		{
			out.println("<form action="+url+" method=post>");
			out.println("<tr>");
			out.println("<th colspan="+row+" class=pager>");
			out.println("<div class=pager>");
			out.println("共"+items.size()+"条记录 每页<input value="+pageSize+" id=pageSize size=2 name='pageSize'/>条");
			out.println("第"+pageNo+"页/共"+totalPages+"页");
			if(pageNo!=1){
				out.println("<a href='"+url+"?pageNo=1'>首页</a>");
				out.println("<a href='"+url+"?pageNo="+(pageNo-1)+"'>上一页</a>");
			}
			if(pageNo!=totalPages){
				out.println("<a href='"+url+"?pageNo="+(pageNo+1)+"'>下一页</a>");
				out.println("<a href='"+url+"?pageNo="+totalPages+"'>尾页</a>");
			}
			out.println("转到<input value=1 size=2 name=pageNo id=pageNo/>页");
			out.println("<input type=submit width=20 value='GO' onclick='return checkPaging()'/>");
			out.println("</div>");
			out.println("<th>");
			out.println("</tr>");
			out.println("</form>");
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	
	
	
	@SuppressWarnings("rawtypes")
	public void setItems(List items) 
	{
		this.items = items;
	}


	public String getVar() 
	{
		return var;
	}


	public int getPageSize() 
	{
		return pageSize;
	}


	public int getPageNo()
	{
		return pageNo;
	}


	public void setVar(String var) 
	{
		this.var = var;
	}


	public void setPageSize(int pageSize) 
	{
		this.pageSize = pageSize;
	}


	public void setPageNo(int pageNo) 
	{
		this.pageNo = pageNo;
	}
	
	
}
